﻿using System;
using System.IO;
using System.Security;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Candy.Resources;
using FluxJpeg.Core;

namespace Candy.Infrastructure
{
    public class BitmapToJpgConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            
            byte[] arr;
            if (value == null)
            {
                //if the value is null then return a default picture
                arr = ImageResources._default;
            }
            else
            {
                arr = (byte[])value;
            }

            //create a in memory image
            MemoryStream str = new MemoryStream(arr);
            BitmapImage img = new BitmapImage();
            img.SetSource(str);
            return img;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            byte[] ret = null;
            WriteableBitmap wb = null;
            if (value is WriteableBitmap)
            {
                wb = value as WriteableBitmap;

            }
            if (value is BitmapImage)
            {
                BitmapImage bmi = value as BitmapImage;
                wb = new WriteableBitmap(bmi);
            }

            if (wb != null)
                ret = WriteablePictureToJpeg(wb);
            return ret;

        }


        private static byte[] WriteablePictureToJpeg(WriteableBitmap wb)
        {
            if (wb != null)
            {
                int width = wb.PixelWidth;
                int height = wb.PixelHeight;
                int bands = 3;
                byte[][,] raster = new byte[bands][,];

                //Convert the Image to pass into FJCore
                //Code From http://stackoverflow.com/questions/1139200/using-fjcore-to-encode-silverlight-writeablebitmap
                for (int i = 0; i < bands; i++)
                {
                    raster[i] = new byte[width, height];
                }

                for (int row = 0; row < height; row++)
                {
                    for (int column = 0; column < width; column++)
                    {
                        int pixel = wb.Pixels[width * row + column];
                        raster[0][column, row] = (byte)(pixel >> 16);
                        raster[1][column, row] = (byte)(pixel >> 8);
                        raster[2][column, row] = (byte)pixel;
                    }
                }

                ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
                FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);

                //Encode the Image as a JPEG
                MemoryStream stream = new MemoryStream();
                FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
                encoder.Encode();

                //Back to the start
                stream.Seek(0, SeekOrigin.Begin);

                //Get the Bytes and write them to the stream
                byte[] binaryData = new Byte[stream.Length];
                long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
                return binaryData;
            }


            return null;
        }

    }
}
